 package CurtainDesign;

 import com.lowagie.text.Document;
 import com.lowagie.text.DocumentException;
 import com.lowagie.text.PageSize;
 import com.lowagie.text.Paragraph;
 import com.lowagie.text.Rectangle;
 import com.lowagie.text.pdf.BaseFont;
 import com.lowagie.text.pdf.PdfPCell;
 import com.lowagie.text.pdf.PdfPTable;
 import com.lowagie.text.pdf.PdfWriter;
 import java.awt.BorderLayout;
 import java.awt.Color;
 import java.awt.FlowLayout;
 import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
 import java.awt.Insets;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Vector;
 import javax.swing.BorderFactory;
 import javax.swing.JButton;
 import javax.swing.JComboBox;
 import javax.swing.JFileChooser;
 import javax.swing.JLabel;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JTextField;
 import javax.swing.border.Border;
 import javax.swing.filechooser.FileFilter;

 public class ShippingMarkPrintView extends JPanel
		 {
	   private JPanel m_MarkPanel = new JPanel();
	   private JPanel m_BtnPanel = new JPanel();
	   private JPanel containerPanel = new JPanel();

	   private JLabel lHDReference = new JLabel("订单编号");
	   private JTextField tHDReference = new JTextField("");

	   private JLabel lCustName = new JLabel("客户名称");
	   private JTextField tCustName = new JTextField("");

	   private JLabel lLocation = new JLabel("房间号");
	   private JTextField tLocation = new JTextField("");

	   private JLabel lMarkOrder = new JLabel("唛头编号");
	   private JTextField tOrder = new JTextField("");
	   private JTextField tTotal = new JTextField("");

	   private JLabel lItem = new JLabel("部件");
	   private JComboBox items = new JComboBox();
	   private JLabel lInputOther = new JLabel("请输入");
	   private JTextField other = new JTextField("");

	   private JLabel lFrameColor = new JLabel("颜色");
	   private JTextField tFrameColor = new JTextField("");

	   private JLabel lOrderDate = new JLabel("下单日期");
	   private JTextField tOrderDate = new JTextField("");

	   private JLabel lCartons = new JLabel("纸盒编号");
	   private JTextField tCartonsIndex = new JTextField("");

	   private JButton btnDefalutPrint = new JButton("默认输出");
	   private JButton btnPre = new JButton("上一页");
	   private JButton btnNext = new JButton("下一页");
	   private JButton btnPrintCurr = new JButton("输出该页");
	   private JButton btnAddNew = new JButton("新增");
	   private JButton btnModify = new JButton("修改");
	   private JButton btnDelete = new JButton("删除");

	   public ShippingMarkPrintView()
	   {
		     jbInit();
		   }

	   private void initData()
	   {
		     MainView.marksList = new ArrayList();

		     Vector tempVct = new Vector();

		     for (int i = 0; i < Constants.SHIPINGITEM.length; i++) {
			       tempVct.add(Constants.SHIPINGITEM[i]);
			       this.items.addItem(tempVct.elementAt(i));
			     }

		     String windowStyle = CurtainDesign.m_UserMainGUI.m_SelectStyle;
		     int totalIndex = 0;
		//外框数量和位置
		     if (CurtainDesign.m_UserMainGUI.GetOrderInfo().GetFrameNumPos() != 2)
			     {
			       totalIndex++;
			       ShippingMarkInfo frameMark = new ShippingMarkInfo();
			       frameMark.setItem(Constants.SHIPINGITEM[0]);//对应唛头 ITEMS ENCLOSED
			       frameMark.setIndex(totalIndex);
			       MainView.marksList.add(frameMark);
			     }

		     int panelNum = new Integer(CurtainDesign.m_UserMainGUI.GetOrderInfo().GetPanelNum()).intValue();//窗户扇数
		     int panelMarkNum = panelNum % 4 == 0 ? panelNum / 4 : panelNum / 4 + 1;
		     int topPanelNum = panelMarkNum;
		//窗户类型 = 普通方形 && 级别or等级 = 2 ？？
		     if ((windowStyle.trim().equalsIgnoreCase(Constants.STYLES[0])) && (CurtainDesign.m_UserMainGUI.GetOrderInfo().GetLevel().trim().equalsIgnoreCase("2")))
			     {
			//底部窗格数量
			       int bottomPanelNum = new Integer(CurtainDesign.m_UserMainGUI.GetOrderInfo().getBottomPanelNum()).intValue();
			       panelMarkNum += (bottomPanelNum % 4 == 0 ? bottomPanelNum / 4 : bottomPanelNum / 4 + 1);
			     }
		     ShippingMarkInfo panelMark = null;
		     for (int i = 0; i < panelMarkNum; i++)
			     {
			       totalIndex++;
			       if (CurtainDesign.m_UserMainGUI.GetOrderInfo().GetLevel().trim().equalsIgnoreCase("2"))
				       {
				         panelMark = new ShippingMarkInfo();
				         panelMark.setItem(Constants.SHIPINGITEM[1]);//对应唛头 ITEMS ENCLOSED
				         panelMark.setIndex(totalIndex);
				         if (i < topPanelNum)
					         {
					           panelMark.setLevelAppendix("T");
					         }
				         else
					         {
					           panelMark.setLevelAppendix("B");
					         }
				         MainView.marksList.add(panelMark);
				       }
			       else {
				         panelMark = new ShippingMarkInfo();
				         panelMark.setItem(Constants.SHIPINGITEM[1]);//对应唛头 ITEMS ENCLOSED
				         panelMark.setIndex(totalIndex);
				         MainView.marksList.add(panelMark);
				       }
			     }
		     MainView.markIndex = 1;

		     this.items.setSelectedItem(((ShippingMarkInfo)MainView.marksList.get(0)).getItem());
		     this.tCartonsIndex.setText(MainView.markIndex + " of " + MainView.marksList.size());//对应唛头 CARTONS 1 of 2  2 of 2
		   }

	   private void jbInit()
	   {
		     initLayout();
		     initListener();
		     this.containerPanel.setLayout(new BorderLayout());
		     this.containerPanel.add(this.m_MarkPanel, "North");
		     this.containerPanel.add(this.m_BtnPanel, "South");
		     add(this.containerPanel, "Center");
		     initData();
		   }

	   private void initLayout()
	   {
		     GridBagLayout gbl = new GridBagLayout();
		     GridBagConstraints gbc = new GridBagConstraints();
		     this.m_MarkPanel.setLayout(gbl);
		     Border border = BorderFactory.createLineBorder(Color.gray, 0);
		     this.m_MarkPanel.setBorder(border);
		     gbc.fill = 1;
		     gbc.anchor = 13;
		     gbc.insets = new Insets(15, 5, 15, 5);

		     gbc.gridwidth = 1;
		     gbc.ipadx = 5;
		     gbc.ipady = 5;
		     this.lHDReference.setFont(new java.awt.Font("Dialog", 0, 13));
		     this.lHDReference.setToolTipText("HD Reference");
		     gbl.setConstraints(this.lHDReference, gbc);
		     this.m_MarkPanel.add(this.lHDReference);

		     gbc.gridwidth = 2;
		     gbc.ipadx = 120;
		     this.tHDReference.setFont(new java.awt.Font("Dialog", 0, 13));
		     if (CurtainDesign.m_UserMainGUI != null) {
			       String pipeIndex = CurtainDesign.m_UserMainGUI.GetOrderInfo().GetOrderIndex();
			       String windowIndex = CurtainDesign.m_UserMainGUI.GetOrderInfo().GetWindowIndex();
			       String windowNum = CurtainDesign.m_UserMainGUI.GetOrderInfo().GetWindowNum();
			       String hdReference = pipeIndex + "-" + windowIndex + "/" + windowNum;
			       this.tHDReference.setText(hdReference);
			     }
		     gbl.setConstraints(this.tHDReference, gbc);
		     this.tHDReference.setEnabled(false);
		     this.m_MarkPanel.add(this.tHDReference);

		     gbc.gridwidth = -1;
		     gbc.ipadx = 5;
		     this.lFrameColor.setFont(new java.awt.Font("Dialog", 0, 13));
		     this.lFrameColor.setToolTipText("Frame color");
		     gbl.setConstraints(this.lFrameColor, gbc);
		     this.m_MarkPanel.add(this.lFrameColor);

		     gbc.gridwidth = 0;
		     gbc.ipadx = 120;
		     this.tFrameColor.setFont(new java.awt.Font("Dialog", 0, 13));
		     this.tFrameColor.setEditable(true);
		     if (CurtainDesign.m_UserMainGUI != null) {
			       String colorStr = CurtainDesign.m_UserMainGUI.GetOrderInfo().GetColor();
			       int index = 0;
			       for (int i = 0; i < Constants.COLOR.length; i++)
				       {
				         if (!Constants.COLOR[i].trim().equalsIgnoreCase(colorStr))
					           continue;
				         index = i;
				         break;
				       }

			       if ((index < 22) || (index > 36))
				       {
				         this.tFrameColor.setText(Constants.COLOR_EN[index] + "(" + Constants.COLOR_SHIPPINGMARK[index] + ")");
				       }
			       else
				       {
				         this.tFrameColor.setText(Constants.COLOR_EN[index]);
				       }
			     }
		     gbl.setConstraints(this.tFrameColor, gbc);
		     this.tFrameColor.setEnabled(false);
		     this.m_MarkPanel.add(this.tFrameColor);

		     gbc.gridwidth = 1;
		     gbc.ipadx = 5;
		     gbc.ipady = 5;
		     this.lCustName.setFont(new java.awt.Font("Dialog", 0, 13));
		     this.lCustName.setToolTipText("Customer Name");
		     gbl.setConstraints(this.lCustName, gbc);
		     this.m_MarkPanel.add(this.lCustName);

		     gbc.gridwidth = 2;
		     gbc.ipadx = 120;
		     this.tCustName.setFont(new java.awt.Font("Dialog", 0, 13));
		     if (CurtainDesign.m_UserMainGUI != null) {
			       this.tCustName.setText(CurtainDesign.m_UserMainGUI.GetOrderInfo().getCustRefNum() + " " + CurtainDesign.m_UserMainGUI.GetOrderInfo().GetClientRegion());
			     }
		     gbl.setConstraints(this.tCustName, gbc);
		     this.tCustName.setEnabled(false);
		     this.m_MarkPanel.add(this.tCustName);

		     gbc.gridwidth = -1;
		     gbc.ipadx = 5;
		     this.lLocation.setFont(new java.awt.Font("Dialog", 0, 13));
		     this.lLocation.setToolTipText("LOCATION");
		     gbl.setConstraints(this.lLocation, gbc);
		     this.m_MarkPanel.add(this.lLocation);

		     gbc.gridwidth = 0;
		     gbc.ipadx = 120;
		     this.tLocation.setFont(new java.awt.Font("Dialog", 0, 13));
		     this.tLocation.setEditable(true);
		     if (CurtainDesign.m_UserMainGUI != null) {
			       this.tLocation.setText(CurtainDesign.m_UserMainGUI.GetOrderInfo().GetRoom());
			     }
		     gbl.setConstraints(this.tLocation, gbc);
		     this.tLocation.setEnabled(false);
		     this.m_MarkPanel.add(this.tLocation);

		     gbc.gridwidth = 1;
		     gbc.ipadx = 5;
		     gbc.ipady = 5;
		     this.lOrderDate.setFont(new java.awt.Font("Dialog", 0, 13));
		     this.lOrderDate.setToolTipText("Customer Name");
		     gbl.setConstraints(this.lOrderDate, gbc);
		     this.m_MarkPanel.add(this.lOrderDate);

		     gbc.gridwidth = 2;
		     gbc.ipadx = 120;
		     this.tOrderDate.setFont(new java.awt.Font("Dialog", 0, 13));
		     if (CurtainDesign.m_UserMainGUI != null) {
			       DateFormat format = new SimpleDateFormat("ddyyyy");
			       Date orderDate = CurtainDesign.m_UserMainGUI.GetOrderInfo().GetOrderDate();
			       this.tOrderDate.setText(format.format(orderDate));
			     }
		     gbl.setConstraints(this.tOrderDate, gbc);
		     this.tOrderDate.setEnabled(false);
		     this.m_MarkPanel.add(this.tOrderDate);

		     gbc.gridwidth = -1;
		     gbc.ipadx = 5;
		     this.lCartons.setFont(new java.awt.Font("Dialog", 0, 13));
		     this.lHDReference.setToolTipText("lCartons");
		     gbl.setConstraints(this.lCartons, gbc);
		     this.m_MarkPanel.add(this.lCartons);

		     gbc.gridwidth = 0;
		     gbc.ipadx = 120;
		     this.tCartonsIndex.setFont(new java.awt.Font("Dialog", 0, 13));
		     gbl.setConstraints(this.tCartonsIndex, gbc);
		     this.tCartonsIndex.setEnabled(false);
		     this.m_MarkPanel.add(this.tCartonsIndex);

		     gbc.gridwidth = 1;
		     gbc.ipadx = 5;
		     gbc.ipady = 5;
		     this.lItem.setFont(new java.awt.Font("Dialog", 0, 13));
		     this.lItem.setToolTipText("ITEMS ENCLOSED");
		     gbl.setConstraints(this.lItem, gbc);
		     this.m_MarkPanel.add(this.lItem);

		     gbc.gridwidth = 2;
		     gbc.ipadx = 120;
		     this.items.setFont(new java.awt.Font("Dialog", 0, 13));
		     gbl.setConstraints(this.items, gbc);
		     this.items.setEnabled(false);
		     this.m_MarkPanel.add(this.items);

		     gbc.gridwidth = -1;
		     gbc.ipadx = 5;
		     this.lInputOther.setFont(new java.awt.Font("Dialog", 0, 13));
		     this.lInputOther.setToolTipText("INPUT PLS.");
		     this.lInputOther.setVisible(false);
		     gbl.setConstraints(this.lInputOther, gbc);
		     this.m_MarkPanel.add(this.lInputOther);

		     gbc.gridwidth = 0;
		     this.other.setFont(new java.awt.Font("Dialog", 0, 13));
		     this.other.setVisible(false);
		     gbl.setConstraints(this.other, gbc);
		     this.other.setEnabled(false);
		     this.m_MarkPanel.add(this.other);

		     this.m_BtnPanel.setLayout(new FlowLayout());
		     this.m_BtnPanel.add(this.btnDefalutPrint);
		   }

	   private void initListener()
	   {
		     this.btnDefalutPrint.addActionListener(new ActionListener()
				     {
			       public void actionPerformed(ActionEvent e)
			       {
				         ShippingMarkPrintView.this.defalutPrint();
				       }
			     });
		   }

	   private void defalutPrint()
	   {
		     try
			     {
			       String m_WorkSheetPath = NaviView.m_WorkSheetPath;
			       JFileChooser jfc = new JFileChooser(m_WorkSheetPath);
			       jfc.setFont(new java.awt.Font("Dialog", 0, 13));
			       jfc.setFileFilter(new FileFilter()
					       {
				         public boolean accept(File f)
				         {
					           boolean accept = true;
					           if (f.isFile())
						           {
						             accept = f.getName().endsWith("pdf");
						           }
					           return accept;
					         }

				         public String getDescription() {
					           return "唛头文件 (*.pdf)";
					         }
				       });
			       String defaultname = "" + CurtainDesign.m_UserMainGUI.GetOrderInfo().GetOrderIndex() + "_";
			       defaultname = defaultname + CurtainDesign.m_UserMainGUI.GetOrderInfo().GetClientRegion() + "_";
			       defaultname = defaultname + CurtainDesign.m_UserMainGUI.GetOrderInfo().GetWindowIndex() + "_";
			       defaultname = defaultname + CurtainDesign.m_UserMainGUI.GetOrderInfo().GetWindowNum() + "_" + "(唛头)";
			       File tmpfile;
			       if ((m_WorkSheetPath == null) || (m_WorkSheetPath.equals("")))
				       {
				         tmpfile = new File(defaultname);
				       }
			       else
				       {
				         String savedir = m_WorkSheetPath.substring(0, m_WorkSheetPath.lastIndexOf("\\"));
				         tmpfile = new File(savedir + "\\" + defaultname);
				       }
			       jfc.setSelectedFile(tmpfile);
			       String filepath = null;
			       if (jfc.showSaveDialog(CurtainDesign.m_UserMainGUI) == 0)
				       {
				         filepath = jfc.getSelectedFile().getPath();
				         if (!filepath.endsWith("pdf"))
					         {
					           filepath = filepath + ".pdf";
					         }
				         File check = new File(filepath);
				         if (check.exists())
					         {
					           int answer = JOptionPane.showConfirmDialog(CurtainDesign.m_UserMainGUI, "该唛头文件已存在，是否要覆盖？");
					           if ((answer == 1) || (answer == 2))
						           {
						             return;
						           }
					         }
				         m_WorkSheetPath = jfc.getSelectedFile().getPath();
				         if (!outAllMark(filepath))
					         {
					           return;
					         }
				         int answer = JOptionPane.showConfirmDialog(CurtainDesign.m_UserMainGUI, "是否现在打开唛头文件？");
				         if ((answer == 1) || (answer == 2))
					         {
					           return;
					         }
				         Process p;
				         try
					         {
					           String strAdobePath = (String)Constants.m_HashConfig.get("Adobe");
					           p = Runtime.getRuntime().exec(strAdobePath + " " + filepath);
					         }
				         catch (IOException f)
					         {
					         }
				       }
			     }
		     catch (Exception ex)
			     {
			       ex.printStackTrace();
			     }
		   }

	   private boolean outAllMark(String filePath)
	   {
		     Rectangle rectPageSize = new Rectangle(PageSize.A4);
		     Document doc = new Document(rectPageSize, 30.0F, 30.0F, 30.0F, 30.0F);
		     try
			     {
			       PdfWriter.getInstance(doc, new FileOutputStream(filePath));

			       doc.open();
			       List markList = MainView.marksList;
			       PdfPTable outTable = null;
			       for (int i = 0; i < markList.size(); i++)
				       {
				         if (i % 2 == 0)
					         {
					           outTable = new PdfPTable(2);
					           outTable.setWidthPercentage(100.0F);
					           float[] widths = { 0.5F, 0.5F };
					           outTable.setWidths(widths);
					           outTable.getDefaultCell().setBorder(0);
					           outMark(doc, (ShippingMarkInfo)markList.get(i), outTable);
					         }
				         else
					         {
					           outMark(doc, (ShippingMarkInfo)markList.get(i), outTable);
					         }

				         if (i % 2 != 0)
					         {
					           doc.add(outTable);
					           doc.add(new Paragraph("\n"));
					         }
				         else if (i == markList.size() - 1)
					         {
					           outTable.addCell(new Paragraph(""));
					           doc.add(outTable);
					         }

				         if ((i == 0) || (i % 8 != 0))
					           continue;
				         doc.newPage();
				       }

			       doc.close();
			     }
		     catch (Exception ex)
			     {
			       ex.printStackTrace();
			       JOptionPane.showMessageDialog(null, ex, "错误", 0);
			       return false;
			     }
		     return true;
		   }

	   private void outMark(Document doc, ShippingMarkInfo markInfo, PdfPTable outTable)
	     throws DocumentException, IOException
	   {
		     BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
		     com.lowagie.text.Font fontChinese = new com.lowagie.text.Font(bfChinese, 8.0F, 0);
		     com.lowagie.text.Font boldFontChinese = new com.lowagie.text.Font(bfChinese, 9.0F, 1);

		     PdfPTable parentTable = new PdfPTable(1);

		     PdfPTable table = new PdfPTable(4);
		     table.setWidthPercentage(100.0F);
		     float[] widths = { 0.25F, 0.25F, 0.25F, 0.25F };
		     table.setWidths(widths);
		     table.getDefaultCell().setBorder(0);
		       //订单编号
		     PdfPCell cell = new PdfPCell();
		     Paragraph para = new Paragraph();
		     cell.addElement(new Paragraph("HD", boldFontChinese));
		     cell.addElement(new Paragraph("REFERENCE：", boldFontChinese));
		     table.addCell(cell);
		     cell = new PdfPCell();
		     cell.addElement(new Paragraph(this.tHDReference.getText().trim(), fontChinese));
		     table.addCell(cell);
		       //item enclosed 根据常亮 Constants.SHIPINGITEM 判断
		     cell = new PdfPCell();
		     cell.addElement(new Paragraph("ITEMS", boldFontChinese));
		     cell.addElement(new Paragraph("ENCLOSED:", boldFontChinese));
		     table.addCell(cell);
		     cell = new PdfPCell();
		     String itemName = markInfo.getItem();//订单信息中的item

		     if (itemName.trim().equals(Constants.SHIPINGITEM[0]))
			     {
			       itemName = "FRAMES AND HARDWARES";
			     }
		     else if (itemName.trim().equals(Constants.SHIPINGITEM[1]))
			     {
			       itemName = "PANELS";
			     }
		     else if (this.other.getText() != null) {
			       itemName = this.other.getText().trim();
			     }

		     cell.addElement(new Paragraph(itemName, fontChinese));
		     table.addCell(cell);
		       //客户名称
		     cell = new PdfPCell();
		     cell.addElement(new Paragraph("CUSTOMER", boldFontChinese));
		     cell.addElement(new Paragraph("NAME：", boldFontChinese));
		     table.addCell(cell);
		     cell = new PdfPCell();
		     cell.addElement(new Paragraph(this.tCustName.getText().trim(), fontChinese));
		     table.addCell(cell);
		       //颜色 订单选择颜色？？PR底漆
		     cell = new PdfPCell();
		     cell.addElement(new Paragraph("COLOUR：", boldFontChinese));
		     table.addCell(cell);
		     cell = new PdfPCell();
		     cell.addElement(new Paragraph(this.tFrameColor.getText().trim(), fontChinese));
		     table.addCell(cell);
		       //位置 主卧 对应订单填写房间信息
		     cell = new PdfPCell();
		     cell.addElement(new Paragraph("LOCATION：", boldFontChinese));
		     table.addCell(cell);
		     cell = new PdfPCell();
		     cell.addElement(new Paragraph(this.tLocation.getText().trim(), fontChinese));
		     table.addCell(cell);
		       //对应订单下单日期
		     cell = new PdfPCell();
		     cell.addElement(new Paragraph("DATE", boldFontChinese));
		     cell.addElement(new Paragraph("ORDERED：", boldFontChinese));
		     table.addCell(cell);
		     cell = new PdfPCell();
		     cell.addElement(new Paragraph(this.tOrderDate.getText().trim(), fontChinese));
		     table.addCell(cell);

		     parentTable.addCell(table);

		     PdfPTable underTable = new PdfPTable(2);
		     underTable.setWidthPercentage(50.0F);
		     widths = new float[] { 0.25F, 0.75F };
		     underTable.setWidths(widths);
		     underTable.getDefaultCell().setBorder(0);
		       //MainView.marksList 唛头信息，根据窗户数量计算生成多少个唛头表格
		     cell = new PdfPCell();
		     cell.addElement(new Paragraph("CARTONS：", boldFontChinese));
		     underTable.addCell(cell);
		     cell = new PdfPCell();
		     cell.addElement(new Paragraph("     " + markInfo.getIndex() + "        of           " + MainView.marksList.size(), fontChinese));
		     if ((null != markInfo.getLevelAppendix()) && (markInfo.getLevelAppendix().trim().length() != 0))
			     {
			       cell.addElement(new Paragraph("                  " + markInfo.getLevelAppendix(), fontChinese));
			     }
		     underTable.addCell(cell);
		     parentTable.addCell(underTable);

		     outTable.addCell(parentTable);
		   }
	 }

/* Location:           C:\Users\Administrator\Desktop\奥特百叶窗\Shutter DesignerV3.1\CurtainDesign.jar
 * Qualified Name:     CurtainDesign.ShippingMarkPrintView
 * JD-Core Version:    0.6.0
 */